﻿using LuShanTea.Admin.Web.App_Start;
using LuShanTea.Admin.Web.Models;
using LuShanTea.DTO;
using LuShanTea.IService;
using LuShanTea.Web.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LuShanTea.Admin.Web.Controllers
{
    public class GoodAttrController : Controller
    {
        public IGoodAttrService goodAttrSer { get; set; }
        // 获取第一页商品属性
        [CheckPermission("GoodAttr.List")]
        [HttpGet]
        public ActionResult List()
        {
            ViewBag.totalCount = goodAttrSer.GetAll().Count();
            var model = goodAttrSer.GetPageData(1);
            return View(model);
        }

        // 获取指定页面下标商品属性
        [CheckPermission("GoodAttr.List")]
        [HttpPost]
        public ActionResult List(int index = 1)
        {
            ViewBag.totalCount = goodAttrSer.GetAll().Count();
            var model = goodAttrSer.GetPageData(index);
            return Json(model);
        }

        // 软删除
        [CheckPermission("GoodAttr.Delete")]
        public ActionResult Delete(long goodAttrId)
        {
            goodAttrSer.MakeDelete(goodAttrId);
            return Json(new AjaxResult { Status = "ok" });
        }

        // 批量删除
        [CheckPermission("GoodAttr.Delete")]
        public ActionResult BatchDel(long[] goodAttrIds)
        {
            foreach (var goodAttrId in goodAttrIds)
            {
                goodAttrSer.MakeDelete(goodAttrId);
            }
            return Json(new AjaxResult { Status = "ok" });
        }

        // 添加商品属性
        [CheckPermission("GoodAttr.Add")]
        [HttpGet]
        public ActionResult AddNew()
        {
            return View();
        }

        // 编辑商品属性
        [CheckPermission("GoodAttr.Edit")]
        [HttpGet]
        public ActionResult Edit(long goodAttrId)
        {
            var model = goodAttrSer.GetGoodAtrById(goodAttrId);
            return View(model);
        }
        [CheckPermission("GoodAttr.Edit")]
        [HttpPost]
        public ActionResult Edit(GoodAttrEditPost model)
        {
            GoodAttrDTO dto = new GoodAttrDTO();
            dto.ArtDesc = model.ArtDesc;
            dto.GoodId = model.GoodId;
            dto.Id = model.Id;
            dto.Level = model.Level;
            dto.ProduceTime = model.ProduceTime;
            dto.ProNumber = model.ProNumber;
            dto.ValidTime = model.ValidTime;
            goodAttrSer.UpdataGoodAttr(dto);
            return Json(new AjaxResult { Status = "ok" });
        }
    }
}